﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace Tezzanator.Net.XNAGUI.Controls
{
    public class ListBox : BaseControl
    {
        private Texture2D BlankPixel;
        Game game;

        public int BorderThickness = 1;
        public Dictionary<string, object> List = new Dictionary<string, object>();
        public Color BorderColor = Color.LightSlateGray;
        public Color InnerColor = new Color(Color.DarkSlateGray.R, Color.DarkSlateGray.G, Color.DarkSlateGray.B, 210);
        public Color SelectedItemColor = Color.DarkBlue;
        public Color TextColor = Color.White;
        public SpriteFont ItemFont; 
        public int ItemHeight = 12;

        public int SelectedIndex = 0;

        private bool LeftWasPressed = false;

        public event ControlEventHandler OnSelectedItemChanged;


        public ListBox(Game thegame)
            : base()
        {
            game = thegame;

            // Create a 1x1 textre to draw some primitives.
            Color[] pixelData2 = new Color[1];
            BlankPixel = new Texture2D(game.GraphicsDevice, 1, 1);
            BlankPixel.GetData<Color>(pixelData2);
            pixelData2[0] = Color.White;
            BlankPixel.SetData<Color>(pixelData2);
        }

        public override void Draw(GameTime gameTime, SpriteBatch spritebatch)
        {

            if (Visible)
            {
                // Draw the border
                spritebatch.Draw(BlankPixel, new Rectangle(
                    (int)AbsoluteScreenPosition.X - BorderThickness,
                    (int)AbsoluteScreenPosition.Y - BorderThickness,
                    (int)Size.X + (BorderThickness * 2),
                    (int)Size.Y + (BorderThickness * 2)), BorderColor);

                // Draw the inner
                spritebatch.Draw(BlankPixel, new Rectangle(
                    (int)AbsoluteScreenPosition.X,
                    (int)AbsoluteScreenPosition.Y,
                    (int)Size.X,
                    (int)Size.Y), InnerColor);

                // Draw Items
                List<String> strs = new List<string>();
                foreach(KeyValuePair<string, object> pairs in List)
                {
                    strs.Add(pairs.Key);
                }

                int y = 0;
                for (int i = 0; i < List.Count; i++)
                {
                    if (i == SelectedIndex)
                    {
                        // Draw Selected marker
                        spritebatch.Draw(BlankPixel, new Rectangle(
                                    (int)AbsoluteScreenPosition.X,
                                    (int)AbsoluteScreenPosition.Y + y,
                                    (int)Size.X,
                                    ItemHeight), SelectedItemColor);
                    }
                    spritebatch.DrawString(ItemFont, strs[i] , new Vector2(AbsoluteScreenPosition.X, AbsoluteScreenPosition.Y + y), TextColor);
                    y += ItemHeight;
                }
               
                base.Draw(gameTime, spritebatch);
            }
        }

        public override void Update(GameTime gameTime)
        {
            if (Visible)
            {
                MouseState mousestate = Mouse.GetState();
                Point mousepos = new Point(mousestate.X, mousestate.Y);
                Vector2 mouseposvec = new Vector2(mousestate.X, mousestate.Y);
                
                Rectangle thisrect = new Rectangle((int)AbsoluteScreenPosition.X, (int)AbsoluteScreenPosition.Y, (int)Size.X, (int)Size.Y);

                if (thisrect.Contains(mousepos))
                {
                    if (mousestate.LeftButton == ButtonState.Pressed && LeftWasPressed == false)
                    {
                        if (gui.InputWasCaught == false)
                        {
                            // Calculate where INSIDE the control the click was, so we can calculate what item was clicked
                            // TODO: SCROLLING
                            Vector2 or = new Vector2((int)AbsoluteScreenPosition.X, (int)AbsoluteScreenPosition.Y);
                            Vector2 Relativemousepos = mouseposvec - or;

                            SelectedIndex = (int)Relativemousepos.Y / ItemHeight;

                            SelectedIndex = (int)MathHelper.Clamp(SelectedIndex, 0, List.Count - 1);

                            //System.Diagnostics.Debug.WriteLine(Relativemousepos);
                            //System.Diagnostics.Debug.WriteLine(SelectedIndex);
                            if (OnSelectedItemChanged != null)
                                OnSelectedItemChanged(this);

                            LeftWasPressed = true;
                        }
                    }

                    if (LeftWasPressed)
                        gui.InputWasCaught = true;

                    if (mousestate.LeftButton == ButtonState.Released && LeftWasPressed == true)
                    {
                        LeftWasPressed = false;
                    }
                }
                else
                {
                    LeftWasPressed = false;
                }

                base.Update(gameTime);
            }
            
        }
    }
}
